/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define LOG_TAG "CONNECTIVITY.utils"
//#define LOG_NDEBUG 0

#include "Utils.h"
#include <android-base/logging.h>
#include <log/log.h>

namespace vendor
{
namespace ecarx
{
namespace xma
{
namespace connectivity
{
namespace V1_0
{
namespace implementation
{

    void Utils::ASN1T2ESIMConnectionStatusResponseValue(
        const ASN1T_OpESIMConnectionStatus_Response& asn, ESIMConnectionStatusResponseValue& data_HAL)
    {
        switch (asn.status)
        {
            case ESIMConnectionStatus::disconnected:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::disconnected;
                break;
            case ESIMConnectionStatus::connecting:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::connecting;
                break;
            case ESIMConnectionStatus::two_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::two_gnetwork;
                break;
            case ESIMConnectionStatus::three_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::three_gnetwork;
                break;
            case ESIMConnectionStatus::four_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::four_gnetwork;
                break;
            case ESIMConnectionStatus::five_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::five_gnetwork;
                break;

            default:
                break;
        }

        data_HAL.networkavailable = asn.networkavailable ? NetworkAvailableStatus::available : NetworkAvailableStatus::notAvailable;

        switch (asn.operator_)
        {
            case ESIMOperatorStatus::noregnetwork:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::noregnetwork;
                break;
            case ESIMOperatorStatus::chinamobile:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::chinamobile;
                break;
            case ESIMOperatorStatus::chinaunicom:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::chinaunicom;
                break;
            case ESIMOperatorStatus::chinatelecom:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::chinatelecom;
                break;

            default:
                break;
        }

        data_HAL.ipadr = (hidl_string)asn.ipadr;

        data_HAL.signalstrength = (int32_t)asn.signalstrength;

        data_HAL.eSimTrafficData.txbytecount = asn.trafficdata.txbytecount;
        data_HAL.eSimTrafficData.rxbytecount = asn.trafficdata.rxbytecount;

        switch (asn.data)
        {
            case DataConnectionStatus::disconnected:
                data_HAL.data = DataConnectionStatus_HAL::disconnected;
                break;
            case DataConnectionStatus::privatedConnected:
                data_HAL.data = DataConnectionStatus_HAL::privatedConnected;
                break;
            case DataConnectionStatus::publicConnected:
                data_HAL.data = DataConnectionStatus_HAL::publicConnected;
                break;
            case DataConnectionStatus::bothConnected:
                data_HAL.data = DataConnectionStatus_HAL::bothConnected;
                break;
            case DataConnectionStatus::singleConnected:
                data_HAL.data = DataConnectionStatus_HAL::singleConnected;
                break;
            case DataConnectionStatus::reserved1:
                data_HAL.data = DataConnectionStatus_HAL::reserved1;
                break;
            case DataConnectionStatus::reserved2:
                data_HAL.data = DataConnectionStatus_HAL::reserved2;
                break;

            default:
                break;
        }

    }

    void Utils::ASN1T2ESIMConnectionStatusNotificationValue(
        const ASN1T_OpESIMConnectionStatus_Notification& asn, ESIMConnectionStatusNotificationValue& data_HAL)
    {
        switch (asn.status)
        {
            case ESIMConnectionStatus::disconnected:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::disconnected;
                break;
            case ESIMConnectionStatus::connecting:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::connecting;
                break;
            case ESIMConnectionStatus::two_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::two_gnetwork;
                break;
            case ESIMConnectionStatus::three_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::three_gnetwork;
                break;
            case ESIMConnectionStatus::four_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::four_gnetwork;
                break;
            case ESIMConnectionStatus::five_gnetwork:
                data_HAL.eSimConnStatus = ESIMConnectionStatus_HAL::five_gnetwork;
                break;

            default:
                break;
        }

        data_HAL.networkavailable = asn.networkavailable ? NetworkAvailableStatus::available : NetworkAvailableStatus::notAvailable;

        switch (asn.operator_)
        {
            case ESIMOperatorStatus::noregnetwork:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::noregnetwork;
                break;
            case ESIMOperatorStatus::chinamobile:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::chinamobile;
                break;
            case ESIMOperatorStatus::chinaunicom:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::chinaunicom;
                break;
            case ESIMOperatorStatus::chinatelecom:
                data_HAL.eSimOpStatus = ESIMOperatorStatus_HAL::chinatelecom;
                break;

            default:
                break;
        }

        data_HAL.ipadr = asn.ipadr;

        data_HAL.signalstrength = (int32_t)asn.signalstrength;

        data_HAL.eSimTrafficData.txbytecount = asn.trafficdata.txbytecount;
        data_HAL.eSimTrafficData.rxbytecount = asn.trafficdata.rxbytecount;

        switch (asn.data)
        {
            case DataConnectionStatus::disconnected:
                data_HAL.data = DataConnectionStatus_HAL::disconnected;
                break;
            case DataConnectionStatus::privatedConnected:
                data_HAL.data = DataConnectionStatus_HAL::privatedConnected;
                break;
            case DataConnectionStatus::publicConnected:
                data_HAL.data = DataConnectionStatus_HAL::publicConnected;
                break;
            case DataConnectionStatus::bothConnected:
                data_HAL.data = DataConnectionStatus_HAL::bothConnected;
                break;
            case DataConnectionStatus::singleConnected:
                data_HAL.data = DataConnectionStatus_HAL::singleConnected;
                break;
            case DataConnectionStatus::reserved1:
                data_HAL.data = DataConnectionStatus_HAL::reserved1;
                break;
            case DataConnectionStatus::reserved2:
                data_HAL.data = DataConnectionStatus_HAL::reserved2;
                break;

            default:
                break;
        }
    }

    void Utils::ASN1T2WLAN_ModeCommand(const ASN1T_OpWLANMode_Response& asn, WLAN_ModeCommand& data_HAL)
    {
        switch (asn.functionalControl)
        {
            case WLAN_Mode::noWLAN:
                data_HAL = WLAN_ModeCommand::noWLAN;
                break;
            case WLAN_Mode::accessPointOn:
                data_HAL = WLAN_ModeCommand::accessPointOn;
                break;
            case WLAN_Mode::staOn:
                data_HAL = WLAN_ModeCommand::staOn;
                break;
            case WLAN_Mode::reserved:
                data_HAL = WLAN_ModeCommand::reserved;
                break;

            default:
                break;
        }
    }

    void Utils::ASN1T2WLANSTAConnectResponseValue(const ASN1T_OpWLANSTAConnect_Response& asn, WLANSTAConnectResponseValue& data_HAL)
    {
        data_HAL.wlApSSID = (char *)asn.wl_AP_SSID;

        if (asn.connectedResult)
        {
            data_HAL.connectedResult = ConnectedResult::connected;
        } else
        {
            data_HAL.connectedResult = ConnectedResult::notConnected;
        }

        if (asn.failure_ReasonCode == 0)
        {
            data_HAL.failureReasonCode = WlanStaConnectFailureReasonCode::noFailure;
        }

    }

} // implementation
} // V1_0
} // connectivity
} // xma
} // ecarx
} // vendor
